package com.animation;

import java.util.Timer;
import java.util.TimerTask;

public class AnimationTimerTask extends TimerTask {
	int[] tempVal = null;
	int[] startVal = null;
	int[] endVal = null;
	int tempNumber;
	int curStep;
	int totalTime;
	CalculateValue.ECurveType typeArray;
	boolean firstRun = true;
	int beginTime;
	Timer tempTimer;
	AnimationStepListener tempListener;
	int curstepseonds;

	public AnimationTimerTask(int[] startArray, int[] endArray,
			CalculateValue.ECurveType AnimationType, int[] valueVector,
			int numberAnimation, int totalseconds, int stepseconds,Timer aTimer,
			AnimationStepListener aListener) {
		tempVal = valueVector;
		startVal = startArray;
		endVal = endArray;
		typeArray = AnimationType;
		tempNumber = numberAnimation;
		totalTime = totalseconds;
		tempTimer = aTimer;
		tempListener = aListener;
		beginTime = stepseconds;

	}

	@Override
	public void run() {
		curStep += beginTime;
		if(curStep  >= totalTime) {
			tempListener.AnimationFinished();
			if(tempTimer != null)
				tempTimer.cancel();
			return;

		}
		for(int i = 0; i < tempNumber; i++) {
			if(endVal[i] == startVal[i]) {
				tempVal[i] = endVal[i];
				if(tempTimer != null) {
					tempTimer.cancel();
				}

			} else {
				int n = endVal[i] - startVal[i];
				double stepVal = CalculateValue.StepValue(curStep, totalTime,
						typeArray);
				tempVal[i] = (int) (startVal[i] + n * stepVal);
				tempListener.AnimationStep();
			}
		}
	}
}